package com.hhly.rabbitmq.routing;

import java.io.IOException;
import java.util.Random;

import com.hhly.rabbitmq.RabbitMQUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
/**
 * 
 * @desc 路由方式与广播的区别在于会根据消息路由发送到指定队列，消费根据路由创建队列获取数据
 * @author jiangwei
 * @date 2017年3月24日
 * @company 益彩网络科技公司
 * @version 1.0
 */
public class EmitLogDirect {
	 static final String EXCHANGE_NAME = "direct_logs";
	 static  final String[] severity = {"error","info","warning"};
	public static void main(String[] args) throws Exception {
		Channel channel = RabbitMQUtil.getChannel();
	    channel.exchangeDeclare(EXCHANGE_NAME, "direct");
	    channel.confirmSelect();
	    channel.addConfirmListener(new ConfirmListener() {
			@Override
			public void handleNack(long deliveryTag, boolean multiple) throws IOException {
				 System.out.println("nack: deliveryTag = "+deliveryTag+" multiple: "+multiple);  
			}
			
			@Override
			public void handleAck(long deliveryTag, boolean multiple) throws IOException {
				 System.out.println("ack: deliveryTag = "+deliveryTag+" multiple: "+multiple);  
			}
		});
	    int i = 0;
	    Random random = new Random();
	    for(;;){
	    	Thread.sleep(1000);
	    	String message = "Hello World!"+ i++;
	    	String routing = severity[random.nextInt(severity.length)];
	 	    channel.basicPublish(EXCHANGE_NAME,routing , null, message.getBytes());
	 	    if(channel.waitForConfirms()){
	 	    	System.out.println(" [x] Sent '" + routing + "':'" + message + "'");	
	 	    }
	    }
	   
	}
}
